//
//  QuestionCollectManager.m
//  保存错题和收藏题，已做过的题
//
//  Created by dcr on 2017/3/9.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "QuestionCollectManager.h"
#define UserDefaults [NSUserDefaults standardUserDefaults]

@implementation QuestionCollectManager

+ (NSArray *) getWrongQuestion{
    NSArray *array = [UserDefaults objectForKey:@"WRONG"];
    if(array != nil)
        return array;
    return @[];
}

+ (Boolean) isExistWrong:(int)mid{
    NSArray* arrays = [QuestionCollectManager getWrongQuestion];
    for(NSString *str in arrays){
        if([str isEqualToString:[NSString stringWithFormat:@"%d", mid]]){
            return true;
        }
    }
    return false;
}

+ (void) addWrongQuestion:(int)mid{
    if([QuestionCollectManager isExistWrong:mid]){
        return;
    }
    NSArray *array = [UserDefaults objectForKey:@"WRONG"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    [arr addObject:[NSString stringWithFormat:@"%d", mid]];
    [UserDefaults setObject:arr forKey:@"WRONG"];
    [UserDefaults synchronize];
}

+ (void) removeWrongQuestion:(int)mid{
    NSArray *array = [UserDefaults objectForKey:@"WRONG"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    for(int i = (int)arr.count-1; i>=0; i--){
        if([arr[i] intValue] == mid){
            [arr removeObjectAtIndex:i];
        }
    }
    [UserDefaults setObject:arr forKey:@"WRONG"];
    [UserDefaults synchronize];
}

+ (NSArray *) getCollectQuestion{
    NSArray *array = [UserDefaults objectForKey:@"COLLECT"];
    if(array != nil)
        return array;
    return @[];
}

+ (Boolean) isExistCollect:(int)mid{
    NSArray* arrays = [QuestionCollectManager getCollectQuestion];
    for(NSString *str in arrays){
        if([str isEqualToString:[NSString stringWithFormat:@"%d", mid]]){
            return true;
        }
    }
    return false;
}

+ (void) addCollectQuestion:(int)mid{
    if([QuestionCollectManager isExistCollect:mid]){
        return;
    }
    NSArray *array = [UserDefaults objectForKey:@"COLLECT"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    [arr addObject:[NSString stringWithFormat:@"%d", mid]];
    [UserDefaults setObject:arr forKey:@"COLLECT"];
    [UserDefaults synchronize];
}

+ (void) removeCollectQuestion:(int)mid{
    NSArray *array = [UserDefaults objectForKey:@"COLLECT"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    for(int i = (int)arr.count-1; i>=0; i--){
        if([arr[i] intValue] == mid){
            [arr removeObjectAtIndex:i];
        }
    }
    [UserDefaults setObject:arr forKey:@"COLLECT"];
    [UserDefaults synchronize];
}


+ (NSArray *) getDoneQuestion{
    NSArray *array = [UserDefaults objectForKey:@"done"];
    if(array != nil)
        return array;
    return @[];
}

+ (Boolean) isExistDone:(int)mid{
    NSArray* arrays = [QuestionCollectManager getDoneQuestion];
    for(NSString *str in arrays){
        if([str isEqualToString:[NSString stringWithFormat:@"%d", mid]]){
            return true;
        }
    }
    return false;
}

+ (void) addDoneQuestion:(int)mid{
    if([QuestionCollectManager isExistDone:mid]){
        return;
    }
    NSArray *array = [UserDefaults objectForKey:@"done"];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
    [arr addObject:[NSString stringWithFormat:@"%d", mid]];
    [UserDefaults setObject:arr forKey:@"done"];
    [UserDefaults synchronize];
}

@end
